package com.byzk.p2p.admin.user.service;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.bank.dao.BankCardDao;
import com.byzk.p2p.admin.bank.dto.BankCard;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.user.dao.UserAuthDao;
import com.byzk.p2p.admin.user.dao.UserDao;
import com.byzk.p2p.admin.user.dto.AuthRecord;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.dto.UserAuth;
import com.byzk.p2p.admin.user.dto.UserDetailInfoAuth;
import com.byzk.p2p.admin.user.dto.UserWorkInfoAuth;

@Service
public class UserAuthService {
	
	@Autowired
	private UserAuthDao userAuthDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private BankCardDao cardDao;
	/**
	 * 根据状态查询认证记录表
	 * @return
	 */
	
	public PageInfo<UserAuth> listUserAuth(UserAuth vo){
		int count=userAuthDao.countByStatus(vo);
		List<UserAuth> voList=userAuthDao.listUserAuth( vo);
		
		PageInfo<UserAuth> page=new PageInfo<UserAuth>(count, vo.getIndex() , vo.getLimit());
		page.setPageList(voList);
		return page;
	}
	
	public UserAuth getUserAuthById(Integer id){
		return userAuthDao.getUserAuthById(id);
	}
	
	public Integer saveOrUpdateUserAuth(UserAuth userAuth){
		if(userAuth.getId()==null){
			return userAuthDao.insert(userAuth);
		}else{
			return userAuthDao.update(userAuth);
		}
	}
	
	public UserAuth getAuthByUidAndType(Integer uid,String authType){
		return userAuthDao.getAuthByUidAndType(uid, authType);
	}
	
	/**
	 * 获取用户认证情况及是否设置支付密码
	 * @param pwd 支付密码
	 * @param phone 手机号
	 * @param mail 邮箱
	 * @param id5  身份证
	 * @param uid
	 * @return
	 */
	public HashMap<String,Integer> getUserAuthDetail(Integer uid){
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		Integer count=0;
		User user=userDao.getById(uid);
		
		if(StringUtils.isBlank(user.getPhone())){
			map.put("phone", 0);
		}else{
			map.put("phone", 1);
			count++;
		}
		UserAuth auth=userAuthDao.getAuthByUidAndType(uid, "id5");
		if(auth==null){
			map.put("id5", 0);
		}else{
			map.put("id5", 1);
			count++;
		}
		UserAuth auth1=userAuthDao.getAuthByUidAndType(uid, "email");
		if(auth1==null){
			map.put("email", 0);
		}else{
			map.put("email", 1);
			count++;
		}
		List<BankCard> cradList=cardDao.listCardByUserId(user.getId());
		if(cradList.size()>0){
			map.put("card", 1);
			count++;
		}else{
			map.put("card", 0);
			
		}		
		map.put("count", count);
		return map;
	}
	
	/**
	 * 保存用户个人详细信息认证
	 * @param userDetailInfoAuth 用户个人详细信息认证
	 * @return
	 */
	public Integer saveOrUpdateUserDetailInfoAuth(UserDetailInfoAuth userDetailInfoAuth){
		if (userDetailInfoAuth.getId() == null) {
			return userAuthDao.insertUserDetailInfoAuth(userDetailInfoAuth);
		}else{
			return userAuthDao.updateUserDetailInfoAuth(userDetailInfoAuth);
		}
	}
	
	/**
	 * 保存用户工作信息认证
	 * @param userWorkInfoAuth 用户工作信息认证
	 * @return
	 */
	public Integer saveOrUpdateUserWorkInfoAuth(UserWorkInfoAuth userWorkInfoAuth){
		if (userWorkInfoAuth.getId() == null) {
			return userAuthDao.insertUserWorkInfoAuth(userWorkInfoAuth);
		}else{
			return userAuthDao.updateUserWorkInfoAuth(userWorkInfoAuth);
		}
	}
	
	/**
	 * 根据用户id获取用户个人详细信息认证
	 * @param userId 用户id
	 * @return
	 */
	public UserDetailInfoAuth getUserDetailInfoAuthByUserId(Integer userId){
		return userAuthDao.getUserDetailInfoAuthByUserId(userId);
	}
	
	/**
	 * 根据用户id用户工作信息认证
	 * @param userId 用户id
	 * @return
	 */
	public UserWorkInfoAuth getUserWorkInfoAuthByUserId(Integer userId){
		return userAuthDao.getUserWorkInfoAuthByUserId(userId);
	}
	
	/**
	 * 验证用户认证身份证是否存在
	 * @param idCard 身份证号
	 * @return
	 */
	public UserAuth getUserAuthByIdCard(String idCard){
		return userAuthDao.getUserAuthByIdCard(idCard);
	}
	/**
	 * 通过用户id号获取用户身份证号码
	 * @param uid
	 * @return
	 */
	public UserAuth getIdNo(Integer uid){
		return userAuthDao.getAuthByUidAndType(uid, "id5");	
	}
	
	public int deleteByUserId(Integer userId) {
		return userAuthDao.deleteByUserId(userId);
	}
	
	public Integer saveOrUpdateAuthRecord(AuthRecord record){
		if(record.getId()==null){
			return userAuthDao.insertRecord(record);
		}else{
			return userAuthDao.updateRecord(record);
		}
	}
	
	public List<AuthRecord> selectByDate(Integer userId,String date){
		return userAuthDao.selectByDate(userId,date);
	}
}
